package com.example.netty

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.netty.ui.model.ClientModel
import com.example.netty.ui.model.ServerModel
import com.example.netty.ui.state.ConnectState
import com.example.netty.ui.theme.NettyDemoTheme
import com.lu.netty.io.core.NettyConnector

private const val TAG = "MainActivity"

class MainActivity : ComponentActivity() {

    private val serverModel by viewModels<ServerModel>()
    private val clientModel by viewModels<ClientModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: isDebug=${com.lu.netty.io.core.NettyConfigure.isDebug}")

        setContent {
            NettyDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    NettyTest()
                }
            }
        }
    }


    @Preview(showBackground = true)
    @Composable
    fun GreetingPreview() {
        NettyDemoTheme {
            NettyTest()
        }
    }

    @Composable
    fun NettyTest() {
        Box(modifier = Modifier.fillMaxSize()) {

            Row {
                ServerLayout()
                ClientLayout()
            }
        }
    }


    @Composable
    private fun ClientLayout() {
        val connectState by clientModel.connectState.collectAsState()
        Column {
            val icon = if (connectState == ConnectState.CONNECTED)
                R.drawable.v_ic_connect_success
            else
                R.drawable.v_ic_connect_failed
            Image(
                modifier = Modifier.size(50.dp),
                painter = painterResource(id = icon),
                contentDescription = null
            )
            Button(onClick = { clientModel.startClient() }) {
                Text(text = "启动客户端")
            }
            Button(onClick = { clientModel.sendMessage("hello i am Client") }) {
                Text(text = "客户端发送数据")
            }
            Button(onClick = { clientModel.stopClient() }) {
                Text(text = "关闭客户端")
            }
        }
    }


    @Composable
    private fun ServerLayout() {

        Column {
            Button(onClick = { serverModel.startServer() }) {
                Text(text = "启动服务端")
            }
            Button(onClick = { serverModel.sendMessage("hello i am Server") }) {
                Text(text = "服务端发送数据")
            }

            Button(onClick = { serverModel.queryClient() }) {
                Text(text = "查看已连接客户端")
            }

            val clientInfo by serverModel.clientList.collectAsState()
            Column {
                clientInfo.forEach {
                    ClientItemInfo(connector = it)
                }
            }
        }
    }

    @Composable
    private fun ClientItemInfo(connector: NettyConnector) {
        Row {
            Text(text = "connector=${connector.key}")
        }
    }
}
